﻿package org.libspark.betweenas3.core.tweens.decorators
{
	import org.libspark.betweenas3.core.tweens.AbstractTween;
	import org.libspark.betweenas3.core.tweens.IITween;
	import org.libspark.betweenas3.core.tweens.TweenDecorator;
	import org.libspark.betweenas3.tweens.ITween;
	
	/**
	 * ITween を指定回数繰り返して実行.
	 * 
	 * @author	yossy:beinteractive
	 */
	public class RepeatedTween extends TweenDecorator
	{
		public function RepeatedTween(baseTween:IITween, repeatCount:uint)
		{
			super(baseTween, 0);
			
			_baseDuration = baseTween.duration;
			_repeatCount = repeatCount;
			_duration = _baseDuration * repeatCount;
		}
		
		private var _baseDuration:Number;
		private var _repeatCount:uint;
		
		public function get repeatCount():uint
		{
			return _repeatCount;
		}
		

		override protected function internalUpdate(time:Number):void 
		{
			if (time >= 0) {
				time -= time < _duration ? _baseDuration * int(time / _baseDuration) : _duration - _baseDuration;
			}
			_baseTween.update(time);
		}
		
		/**
		 * @inheritDoc
		 */
		override protected function newInstance():AbstractTween 
		{
			return new RepeatedTween(_baseTween.clone() as IITween, repeatCount);
		}
	}
}